import httpRequest from './service'
import {getTerminal} from "@/utils/terminal";


const options = {
    // 显示操作成功消息 默认不显示
    showSuccess: false,
    // 成功提醒 默认使用后端返回值
    successMsg: '',
    // 显示失败消息 默认显示
    showError: true,
    // 失败提醒 默认使用后端返回信息
    errorMsg: '',
    // 显示请求时loading模态框 默认显示
    showLoading: true,
    // loading提醒文字
    loadingMsg: '加载中..',
    // 需要授权才能请求 默认放开
    auth: false,
    // 是否传递 token
    isToken: true,
};

const request = (option: any) => {
    const {headersType, headers, custom, ...otherOption} = option
    return httpRequest({
        ...otherOption,
        custom: {...options, ...custom},
        headers: {
            'Content-Type': headersType || import.meta.env.VITE_DEFAULT_HEADER,
            "terminal": getTerminal().toString(),
            ...headers
        }
    })
}

export default {
    get: async <T = any>(option: any): Promise<any> =>
        await request({method: 'GET', ...option}) as unknown as Promise<T>,

    post: async <T = any>(option: any): Promise<any> =>
        await request({method: 'POST', ...option}) as unknown as Promise<T>,

    put: async <T = any>(option: any): Promise<any> =>
        await request({method: 'PUT', ...option}) as unknown as Promise<T>,

    delete: async <T = any>(option: any): Promise<any> =>
        await request({method: 'DELETE', ...option}) as unknown as Promise<T>,

    download: async <T = any>(option: any) =>
        await request({method: 'GET', responseType: 'blob', ...option}) as unknown as Promise<T>,

    upload: async <T = any>(option: any) =>
        await request({method: 'POST', headersType: 'multipart/form-data', ...option}) as unknown as Promise<T>,

}
